<?php
require_once IA_ROOT . '/addons/goouc_class/inc/common/wxpay/log.php';
require_once IA_ROOT . '/addons/goouc_class/inc/common/wxpay/WxPayConfig.php';
require_once IA_ROOT . '/addons/goouc_class/inc/common/wxpay/lib/WxPayData.php';
require_once IA_ROOT . '/addons/goouc_class/inc/common/wxpay/lib/WxPayApi.php';
global $_W, $_GPC;
$contion = " p.weid = :weid";
$params[':weid'] = $_W['uniacid'];
$op = empty($_GPC['op']) ? 'payment' : $_GPC['op'];
if ($op == 'payment') {
    $type = intval($_GPC['type']);
    if ($type == 1) {
        $contion .= " AND p.type = :type";
        $params[':type'] = $type;
    } elseif ($type == 2) {
        $contion .= " AND (p.type = 2 OR p.type = 3)";
    }
    $order_status = intval($_GPC['order_status']);
    if ($order_status == 1 || $order_status == 2) {
        $contion .= " AND p.order_status = :order_status";
        if ($order_status == 1) {
            $params[':order_status'] = 1;
        } elseif ($order_status == 2) {
            $params[':order_status'] = 0;
        }
    }
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']) + 86399;
        $contion .= " AND p.createtime >= :starttime AND p.createtime <= :endtime";
        $params[':starttime'] = $starttime;
        $params[':endtime'] = $endtime;
    } else {
        $starttime = strtotime('-1 month');
        $endtime = time();
    }
    $group_status = intval($_GPC['group_status']);
    if ($order_status == 1 || $order_status == 2) {
        $contion .= " AND p.order_status = :order_status";
        if ($order_status == 1) {
            $params[':order_status'] = 1;
        } elseif ($order_status == 2) {
            $params[':order_status'] = 0;
        }
    }
    $pindex = max(1, $_GPC['page']);
    $psize = 15;
    $list = pdo_fetchall("SELECT p.out_trade_no,p.id,p.type,p.order_status,p.createtime,p.paytime,p.dataid,u.nickname,u.headimg,u.phone FROM " . tablename('goouc_class_order') . " p JOIN " . tablename('goouc_class_user') . " u ON p.userid = u.id WHERE " . $contion . " ORDER BY createtime DESC LIMIT " . ($pindex - 1) * $psize . "," . $psize, $params);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('goouc_class_order') . " p JOIN " . tablename('goouc_class_user') . " u ON p.userid = u.id WHERE " . $contion, $params);
    $page = pagination($total, $pindex, $psize);
    $money = pdo_fetchcolumn('SELECT sum(true_money) as money FROM ' . tablename('goouc_class_order') . ' WHERE order_status = :order_status AND weid = :weid', [':order_status' => 1, ':weid' => $_W['uniacid']]);
    foreach ($list as $k => $v) {
        $list[$k]['nickname'] = json_decode($list[$k]['nickname']);
        $lessoninfo = pdo_fetch("select * from " . tablename($this->t_lesson) . " where id = :id ", array(':id' => $v['dataid']));
        $list[$k]['title'] = $lessoninfo['title'];
        $list[$k]['starttime'] = $lessoninfo['addtime'];
        $list[$k]['sonnum'] = pdo_fetchcolumn("select COUNT(*) from " . tablename($this->t_son) . " where lessonid = :lessonid", [':lessonid' => $lessoninfo['id']]);
        //拼团的信息
        if ($v['type'] == 2 || $v['type'] == 3) {
            //直接使用订单号查询团
            $groupinfo = pdo_fetch("select * from " . tablename($this->t_user_group_info) . " ugi JOIN " . tablename($this->t_user_group) . " ug ON ugi.groupid = ug.id where ugi.out_trade_no = :out_trade_no ", [':out_trade_no' => $v['out_trade_no']]);
            if ($groupinfo['status'] == 1) {
                $list[$k]['groupstatus'] = '拼团成功';
            } elseif ($groupinfo['status'] == 2) {
                $list[$k]['groupstatus'] = '拼团中';
            } else {
                $list[$k]['groupstatus'] = '拼团失败';
                if ($groupinfo['refund_status'] == 2) {
                    $list[$k]['refund_status'] = '未退费';//
                } else {
                    $list[$k]['refund_status'] = '已退费';//已退费
                }
            }
        }
    }
} elseif ($op == 'group') {
    $pindex = max(1, $_GPC['page']);
    $psize = 15;
    //获得所有 过期未完成的团
    $list = pdo_fetchall("select ug.id,ug.createtime,ug.status,ug.groupnum,ug.lessonid,ug.status,u.nickname,u.headimg,u.myheaderimg from " . tablename($this->t_user_group) . " ug JOIN " . tablename($this->t_user) . " u ON ug.uid = u.id where ug.weid = :weid  order by ug.createtime DESC limit " . ($pindex - 1) * $psize . "," . $psize, array(':weid' => $_W['uniacid']));
    $total = pdo_fetchcolumn("select COUNT(*) from " . tablename($this->t_user_group) . " ug JOIN " . tablename($this->t_user) . " u ON ug.uid = u.id where ug.weid = :weid ", array(':weid' => $_W['uniacid']));
    foreach ($list as $k => $v) {
        if (!empty($v['myheaderimg'])) {
            $list[$k]['headerimg'] = $v['myheaderimg'];
        } else {
            $list[$k]['headerimg'] = $v['headimg'];
        }
        unset($list[$k]['myheaderimg']);
        unset($list[$k]['headimg']);
        $list[$k]['title'] = pdo_fetchcolumn("select title from " . tablename($this->t_lesson) . " where id = :id ", array(':id' => $v['lessonid']));
    }
    $page = pagination($total, $pindex, $psize);
} elseif ($op == 'refund') {
    if ($_W['isajax']) {
        $groupid = $_GPC['groupid'];
        $userlist = pdo_fetchall("select o.out_trade_no,o.transaction_sn,o.all_money,o.true_money,o.order_status,ugi.uid,o.id from " . tablename($this->t_user_group_info) . " ugi JOIN " . tablename($this->t_order) . " o ON ugi.out_trade_no = o.out_trade_no where ugi.groupid = :groupid and ugi.weid = :weid", array(':groupid' => $groupid, ':weid' => $_W['uniacid']));
        foreach ($userlist as $k => $v) {
            if ($v['order_status'] == 1) {
                $out_trade_no = $v['out_trade_no'];
                $total_fee = $v['true_money'] * 100;
                $refund_fee = $v['true_money'] * 100;
                $input = new WxPayRefund();
                $input->SetOut_trade_no($out_trade_no);
                $input->SetTotal_fee($total_fee);
                $input->SetRefund_fee($refund_fee);
                $config = new WxPayConfig();
                $input->SetOut_refund_no("refund" . date("YmdHis"));
                $input->SetOp_user_id($config->GetMerchantId());
                $result = WxPayApi::refund($config, $input);
                if ($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
                    pdo_update($this->t_order, array('order_status' => 2, 'endtime' => TIMESTAMP), array('id' => $v['id']));
                }
            }
        }
        $message = array('success' => 200, 'msg' => '退款成功');
        echo json_encode($message);
        exit;
    }
} elseif ($op == 'download') {
    $type = intval($_GPC['type']);
    if ($type == 1) {
        $contion .= " AND p.type = :type";
        $params[':type'] = $type;
    } elseif ($type == 2) {
        $contion .= " AND (p.type = 2 OR p.type = 3)";
    }
    $order_status = intval($_GPC['order_status']);
    if ($order_status == 1 || $order_status == 2) {
        $contion .= " AND p.order_status = :order_status";
        if ($order_status == 1) {
            $params[':order_status'] = 1;
        } elseif ($order_status == 2) {
            $params[':order_status'] = 0;
        }
    }
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['starttime']);
        $endtime = strtotime($_GPC['time']['endtime']) + 86399;
        $contion .= " AND p.createtime >= :starttime AND p.createtime <= :endtime";
        $params[':starttime'] = $starttime;
        $params[':endtime'] = $endtime;
    } else {
        $starttime = strtotime('-1 month');
        $endtime = time();
    }
    $group_status = intval($_GPC['group_status']);
    if ($order_status == 1 || $order_status == 2) {
        $contion .= " AND p.order_status = :order_status";
        if ($order_status == 1) {
            $params[':order_status'] = 1;
        } elseif ($order_status == 2) {
            $params[':order_status'] = 0;
        }
    }
    $list = pdo_fetchall("SELECT p.out_trade_no,p.id,p.type,p.order_status,p.createtime,p.paytime,p.dataid,u.nickname,u.headimg,u.phone FROM " . tablename('goouc_class_order') . " p JOIN " . tablename('goouc_class_user') . " u ON p.userid = u.id WHERE " . $contion . " ORDER BY createtime DESC  ", $params);
    foreach ($list as $k => $v) {
        $list[$k]['nickname'] = $this->removeEmoji(json_decode($list[$k]['nickname']));
        $lessoninfo = pdo_fetch("select * from " . tablename($this->t_lesson) . " where id = :id ", array(':id' => $v['dataid']));
        $list[$k]['title'] = $lessoninfo['title'];
        $list[$k]['starttime'] = $lessoninfo['addtime'];
        $list[$k]['sonnum'] = pdo_fetchcolumn("select COUNT(*) from " . tablename($this->t_son) . " where lessonid = :lessonid", [':lessonid' => $lessoninfo['id']]);
        //拼团的信息
        if ($v['type'] == 2 || $v['type'] == 3) {
            $list[$k]['type_name'] = '拼团购买';
            //直接使用订单号查询团
            $groupinfo = pdo_fetch("select * from " . tablename($this->t_user_group_info) . " ugi JOIN " . tablename($this->t_user_group) . " ug ON ugi.groupid = ug.id where ugi.out_trade_no = :out_trade_no ", [':out_trade_no' => $v['out_trade_no']]);
            if ($groupinfo['status'] == 1) {
                $list[$k]['groupstatus'] = '拼团成功';
            } elseif ($groupinfo['status'] == 2) {
                $list[$k]['groupstatus'] = '拼团中';
            } else {
                $list[$k]['groupstatus'] = '拼团失败';
                if ($groupinfo['refund_status'] == 2) {
                    $list[$k]['refund_status'] = '未退费';//
                } else {
                    $list[$k]['refund_status'] = '已退费';//已退费
                }
            }
        } elseif ($v['type'] == 1) {
            $list[$k]['type_name'] = '原价购买';
        } elseif ($v['type'] == 4) {
            $list[$k]['type_name'] = '优惠购买';
        }
    }
    $name = '订单信息';
    //准备开始输出表格
    $objPHPExcel = new PHPExcel;
    $objPHPExcel->getProperties()->setCreator("zxf")->setLastModifiedBy("zxf")->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('导出数据')->setKeywords("excel")->setCategory("result file");
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);//行高
    $objPHPExcel->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('1')->getFont()->setSize(14);     //字体大小
    $objPHPExcel->getActiveSheet()->getStyle('1')->getFont()->setBold(true);      //第一行是否加粗
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(45);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->setCellValue('A1', '购买者');
    $objPHPExcel->getActiveSheet()->setCellValue('B1', '手机号');
    $objPHPExcel->getActiveSheet()->setCellValue('C1', '购买课程');
    $objPHPExcel->getActiveSheet()->setCellValue('D1', '开课时间');
    $objPHPExcel->getActiveSheet()->setCellValue('E1', '课次');
    $objPHPExcel->getActiveSheet()->setCellValue('F1', '购买类型');
    $objPHPExcel->getActiveSheet()->setCellValue('G1', '下单时间');
    $objPHPExcel->getActiveSheet()->setCellValue('H1', '支付时间');
    $objPHPExcel->getActiveSheet()->setCellValue('I1', '拼团状态');
    $objPHPExcel->getActiveSheet()->setCellValue('J1', '退款状态');
    $key = 2;
    foreach ($list as $k => $v) {
        $objPHPExcel->getActiveSheet()->setCellValue('A' . $key, $v['nickname']);
        $objPHPExcel->getActiveSheet()->setCellValue('B' . $key, $v['phone']);
        $objPHPExcel->getActiveSheet()->setCellValue('C' . $key, $v['title']);
        $objPHPExcel->getActiveSheet()->setCellValue('D' . $key, date('Y/m/d H:i:s', $v['starttime']));
        $objPHPExcel->getActiveSheet()->setCellValue('E' . $key, $v['sonnum']);
        $objPHPExcel->getActiveSheet()->setCellValue('F' . $key, $v['type_name']);
        $objPHPExcel->getActiveSheet()->setCellValue('G' . $key, date('Y/m/d H:i:s', $v['createtime']));
        $objPHPExcel->getActiveSheet()->setCellValue('H' . $key, empty($v['paytime']) ? '未支付' : date('Y/m/d H:i:s', $v['paytime']));
        $objPHPExcel->getActiveSheet()->setCellValue('I' . $key, $v['groupstatus']);
        $objPHPExcel->getActiveSheet()->setCellValue('J' . $key, $v['refund_status']);
        $key++;
    }
    ob_end_clean();
    header('Content-Type: application/vnd.ms-excel'); //文件类型
    header('Content-Disposition: attachment;filename="' . $name . '.xls"'); //文件名
    header('Cache-Control: max-age=0');
    header('Content-Type: text/html; charset=utf-8'); //编码
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel 2003
    $objWriter->save('php://output');
    exit;
}
include $this->template('web/payment');
?>